Public Class frmManageEKGAbnormal

#Region "Declare Variables"
    Private m_eScreenMode As eScreenMode
    Private m_EKGAbnormal As New PoliceHealthClass.EKGAbnormal
    Private m_ManageEKGAbnormalControl As New PoliceHealthClass.ManageReferrenceControl
    Private IsPostBack As Boolean = False

    Private cbbStoolExamResultHistory As ComboBox

    Delegate Sub delLoadDatagridviewEKGAbnormal(ByVal intSelectRowIndex As Integer)
#End Region

#Region "Enumuration"
    Private Enum eScreenMode As Integer
        Initial
        View
        Add
        Edit
    End Enum
#End Region

#Region "General Functions"
    Private Sub SetScreenMode(ByVal ScreenMode As eScreenMode)
        m_eScreenMode = ScreenMode
        Select Case ScreenMode
            Case eScreenMode.Initial
                PoliceHealthClass.Utilities.EnableControl(False, btnSave, btnCancel, btnDelete, btnEdit, txtEKGAbnormalNameTha, txtEKGAbnormalNameEng)
                PoliceHealthClass.Utilities.EnableControl(True, btnAdd)

                dgvEKGAbnormal.Enabled = True

                If Not Me.IsPostBack Then

                    ConnectionString.ShowLoadingForm()
                    Me.Invoke(New delLoadDatagridviewEKGAbnormal(AddressOf LoadDatagridviewEKGAbnormal), 0)
                    ConnectionString.TerminateLoadingForm()

                    'LoadDatagridviewEKGAbnormal()
                    Me.IsPostBack = True
                End If

            Case eScreenMode.View
                PoliceHealthClass.Utilities.EnableControl(False, btnSave, btnCancel, txtEKGAbnormalNameTha, txtEKGAbnormalNameEng)
                PoliceHealthClass.Utilities.EnableControl(True, btnEdit, btnDelete, btnAdd)

                dgvEKGAbnormal.Enabled = True
            Case eScreenMode.Add
                PoliceHealthClass.Utilities.EnableControl(True, btnSave, btnCancel, txtEKGAbnormalNameTha, txtEKGAbnormalNameEng)
                PoliceHealthClass.Utilities.EnableControl(False, btnEdit, btnDelete, btnAdd)

                ClearFormData()
                dgvEKGAbnormal.Enabled = False

            Case eScreenMode.Edit
                PoliceHealthClass.Utilities.EnableControl(True, btnSave, btnCancel, txtEKGAbnormalNameTha, txtEKGAbnormalNameEng)
                PoliceHealthClass.Utilities.EnableControl(False, btnEdit, btnDelete, btnAdd)

                dgvEKGAbnormal.Enabled = False
        End Select


    End Sub


    Private Function VerifyInputData() As Boolean
        Return m_ManageEKGAbnormalControl.verifyInsert()
    End Function

    Private Sub LoadDatagridviewEKGAbnormal(Optional ByVal intSelectRowIndex As Integer = 0)
        dgvEKGAbnormal.Enabled = True
        m_EKGAbnormal.LoadEKGAbnormal()

        Dim intRowCount As Integer = 0
        dgvEKGAbnormal.Rows.Clear()
        While m_EKGAbnormal.NextRecord
            dgvEKGAbnormal.Rows.Add()
            dgvEKGAbnormal.Rows(intRowCount).Cells("Order").Value = intRowCount + 1
            dgvEKGAbnormal.Rows(intRowCount).Cells("EKGAbnormalID").Value = m_EKGAbnormal.ekg_abnormal_id
            dgvEKGAbnormal.Rows(intRowCount).Cells("EKGAbnormalNameThai").Value = m_EKGAbnormal.ekg_abnormal_name_tha
            dgvEKGAbnormal.Rows(intRowCount).Cells("EKGAbnormalNameEng").Value = m_EKGAbnormal.ekg_abnormal_name_eng


            dgvEKGAbnormal.Rows(intRowCount).Selected = False

            intRowCount += 1
        End While

        If intSelectRowIndex < dgvEKGAbnormal.Rows.Count Then
            dgvEKGAbnormal.Rows(intSelectRowIndex).Selected = True
            dgvEKGAbnormal.CurrentCell = dgvEKGAbnormal.Rows(intSelectRowIndex).Cells(0)
        End If

        If dgvEKGAbnormal.Rows.Count > 0 Then
            SetScreenMode(eScreenMode.View)
            LoadDataForm(dgvEKGAbnormal.Rows(intSelectRowIndex).Cells("EKGAbnormalID").Value)
        Else
            If Me.IsPostBack Then
                SetScreenMode(eScreenMode.Initial)
            End If
            ClearFormData()
        End If
    End Sub

    Private Sub LoadDataForm(ByVal EKGAbnormalID As String)
        txtEKGAbnormalID.Text = EKGAbnormalID

        'Load Form
        If dgvEKGAbnormal.SelectedRows.Count > 0 Then
            txtEKGAbnormalNameTha.Text = dgvEKGAbnormal.SelectedRows(0).Cells("EKGAbnormalNameThai").Value
            txtEKGAbnormalNameEng.Text = dgvEKGAbnormal.SelectedRows(0).Cells("EKGAbnormalNameEng").Value
        Else
            txtEKGAbnormalNameTha.Text = dgvEKGAbnormal.Rows(0).Cells("EKGAbnormalNameThai").Value
            txtEKGAbnormalNameEng.Text = dgvEKGAbnormal.Rows(0).Cells("EKGAbnormalNameEng").Value
        End If

    End Sub

    Private Sub ClearFormData()
        txtEKGAbnormalNameTha.Text = ""
        txtEKGAbnormalNameEng.Text = ""
        PoliceHealthClass.Utilities.EnableControl(False, btnEdit)
    End Sub






#End Region

#Region "Event Handling Functions"
    Private Sub frmPolice_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'CMU_KPI_PHASE_1DataSet.tbError_Logging' table. You can move, or remove it, as needed.
        SetScreenMode(eScreenMode.Initial)
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        If dgvEKGAbnormal.SelectedRows.Count > 0 Then
            LoadDataForm(dgvEKGAbnormal.Rows(0).Cells("EKGAbnormalID").Value)
            SetScreenMode(eScreenMode.View)
        Else
            SetScreenMode(eScreenMode.Initial)
        End If
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim SelectIndex As Integer = 0
        Dim blVerifyInputControl As Boolean = False


        If m_eScreenMode = eScreenMode.Edit OrElse m_eScreenMode = eScreenMode.Add Then
            If VerifyInputData() Then

                If dgvEKGAbnormal.SelectedRows.Count > 0 Then
                    SelectIndex = dgvEKGAbnormal.SelectedRows(0).Index
                End If


                m_EKGAbnormal.ekg_abnormal_id = PoliceHealthClass.Utilities.ClearEmptyToMinValue(txtEKGAbnormalID.Text, TypeCode.Int32)
                m_EKGAbnormal.ekg_abnormal_name_tha = txtEKGAbnormalNameTha.Text
                m_EKGAbnormal.ekg_abnormal_name_eng = txtEKGAbnormalNameEng.Text



                If m_eScreenMode = eScreenMode.Edit Then
                    m_ManageEKGAbnormalControl.showInsertResult(m_EKGAbnormal.updateEKGAbnormal(m_EKGAbnormal))
                ElseIf m_eScreenMode = eScreenMode.Add Then
                    m_ManageEKGAbnormalControl.showInsertResult(m_EKGAbnormal.insertEKGAbnormal(m_EKGAbnormal))
                End If

                ConnectionString.ShowLoadingForm()
                Me.Invoke(New delLoadDatagridviewEKGAbnormal(AddressOf LoadDatagridviewEKGAbnormal), dgvEKGAbnormal.SelectedRows(0).Index)
                ConnectionString.TerminateLoadingForm()

                'LoadDatagridviewEKGAbnormal(dgvEKGAbnormal.SelectedRows(0).Index)

            End If
        End If
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        SetScreenMode(eScreenMode.Add)
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        SetScreenMode(eScreenMode.Edit)
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        If m_ManageEKGAbnormalControl.verifyDelete() Then
            m_EKGAbnormal.ekg_abnormal_id = txtEKGAbnormalID.Text
            m_ManageEKGAbnormalControl.showDeleteResult(m_EKGAbnormal.deleteEKGAbnormal(m_EKGAbnormal))

            ConnectionString.ShowLoadingForm()
            Me.Invoke(New delLoadDatagridviewEKGAbnormal(AddressOf LoadDatagridviewEKGAbnormal), 0)
            ConnectionString.TerminateLoadingForm()

            'LoadDatagridviewEKGAbnormal()
        End If
    End Sub

    Private Sub dgvEKGAbnormal_RowEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvEKGAbnormal.RowEnter
        LoadDataForm(dgvEKGAbnormal.Rows(e.RowIndex).Cells("EKGAbnormalID").Value)
    End Sub
#End Region
End Class